<?php
namespace Card\Controller;

use Base\Controller\AdminBaseController;

class CardController extends AdminBaseController {

	protected $cardModel;

	public function _initialize() {
		parent::_initialize();
		
		$this->cardModel = D("Card/Card");
	}
	
	/******************************** JSON *********************************/
	/** 添加信息 */
	public function save() {
		$cardJson = file_get_contents('php://input');
		$card = json_decode($cardJson, true);
		
		// 检查名称是否重复
		$oldCard = $this->cardModel->where(array("no" => $card['no']))->find();
		if ($oldCard) {
			$data['code'] = 499;
			$data['msg'] = '添加失败，该会员卡号已存在!';
			$this->ajaxReturn($data);
		}
		
		// setting
// 		$card['status'] = 0;
		$card['add_time'] = Date('Y-m-d H:i:s');
		$card['update_time'] = Date('Y-m-d H:i:s');

		// 执行添加
		$result = $this->cardModel->data($card)->add();
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '添加失败！';
			$this->ajaxReturn($data);
		}

		$newCard = $this->cardModel->where(array("id" => $result))->find();
		
		$data['code'] = 0;
		$data['msg'] = '添加成功！';
		$data['obj'] = $newCard;
		$this->ajaxReturn($data);
	}
	 
	/** 查看某信息 */
	public function find() {
		$id = $_GET['id'];
		
		$card = $this->cardModel->where(array("id" => $id))->find();
		if (!$card) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
	    $data['code'] = 0;
	    $data['msg'] = 'OK';
	    $data['obj'] = $card;
	    $this->ajaxReturn($data);
	}

	/** 查看所有列表 */
	public function dataList() {
		$_GET['notEqualMap'] = ['status' => -1];
		$dataList = $this->cardModel->dataList($_GET);
	    $this->ajaxReturn($dataList);
	}

	/** 修改某信息 */
	public function update() {
		$id = $_GET['id'];
		$cardJson = file_get_contents('php://input');
		$card = json_decode($cardJson, true);

		// update
// 		$card['status'] = 0;										// update reset
		$card['update_time'] = date("Y-m-d H:i:s");
		
		// 执行更新
		$result = $this->cardModel->where(array("id" => $id))->save($card);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '编辑失败！';
			$this->ajaxReturn($data);
		}

		$updatedCard = $this->cardModel->where(array("id" => $id))->find();
		
		$data['code'] = 0;
		$data['msg'] = '编辑成功！';
		$data['obj'] = $updatedCard;
		$this->ajaxReturn($data);
	}
	
	/** 删除某信息 */
	public function delete() {
		$id = $_GET['id'];
		
		$card = $this->cardModel->where(array("id" => $id))->find();
		if (!$card) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}

		$customer = M('Customer')->where(array("card_id" => $id))->find();
		if ($customer) {
			$data['code'] = 499;
			$data['msg'] = '删除失败，该卡为用户持有！';
			$this->ajaxReturn($data);
		}
        
		if (!$this->cardModel->delete($id)) {
			$data['code'] = 499;
			$data['msg'] = '删除失败！';
			$this->ajaxReturn($data);
		}
			
		$data['code'] = 0;
		$data['msg'] = '删除成功！';
		$this->ajaxReturn($data);
	}

	/******************************** HTML *********************************/
	/** index */
    public function index() {
		$this->display(':card');
		return;
	}
	
}